package com.example.xmltohtml.service;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class PdfToHtmlService {

    /**
     * 将PDF文件转换为HTML格式
     * @param pdfFile PDF文件
     * @return HTML字符串
     */
    public String convertPdfToHtml(MultipartFile pdfFile) throws IOException {
        try (PDDocument document = PDDocument.load(pdfFile.getInputStream())) {
            PDFTextStripper stripper = new PDFTextStripper();
            String text = stripper.getText(document);
            
            return generateHtmlFromText(text, pdfFile.getOriginalFilename());
        }
    }

    /**
     * 将PDF文件路径转换为HTML格式
     * @param pdfPath PDF文件路径
     * @return HTML字符串
     */
    public String convertPdfToHtml(String pdfPath) throws IOException {
        Path path = Paths.get(pdfPath);
        try (PDDocument document = PDDocument.load(path.toFile())) {
            PDFTextStripper stripper = new PDFTextStripper();
            String text = stripper.getText(document);
            
            return generateHtmlFromText(text, path.getFileName().toString());
        }
    }

    /**
     * 从文本生成HTML
     * @param text 提取的文本
     * @param fileName 原文件名
     * @return HTML字符串
     */
    private String generateHtmlFromText(String text, String fileName) {
        StringBuilder html = new StringBuilder();
        
        html.append("<!DOCTYPE html>\n");
        html.append("<html lang=\"zh-CN\">\n");
        html.append("<head>\n");
        html.append("    <meta charset=\"UTF-8\">\n");
        html.append("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        html.append("    <title>").append(fileName).append(" - PDF转换结果</title>\n");
        html.append("    <style>\n");
        html.append("        body {\n");
        html.append("            font-family: 'Microsoft YaHei', Arial, sans-serif;\n");
        html.append("            line-height: 1.6;\n");
        html.append("            margin: 0;\n");
        html.append("            padding: 20px;\n");
        html.append("            background-color: #f5f5f5;\n");
        html.append("        }\n");
        html.append("        .container {\n");
        html.append("            max-width: 800px;\n");
        html.append("            margin: 0 auto;\n");
        html.append("            background-color: white;\n");
        html.append("            padding: 30px;\n");
        html.append("            border-radius: 8px;\n");
        html.append("            box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n");
        html.append("        }\n");
        html.append("        .header {\n");
        html.append("            border-bottom: 2px solid #007bff;\n");
        html.append("            padding-bottom: 15px;\n");
        html.append("            margin-bottom: 20px;\n");
        html.append("        }\n");
        html.append("        .header h1 {\n");
        html.append("            color: #007bff;\n");
        html.append("            margin: 0;\n");
        html.append("        }\n");
        html.append("        .content {\n");
        html.append("            white-space: pre-wrap;\n");
        html.append("            font-size: 14px;\n");
        html.append("            color: #333;\n");
        html.append("        }\n");
        html.append("        .footer {\n");
        html.append("            margin-top: 30px;\n");
        html.append("            padding-top: 15px;\n");
        html.append("            border-top: 1px solid #eee;\n");
        html.append("            color: #666;\n");
        html.append("            font-size: 12px;\n");
        html.append("        }\n");
        html.append("    </style>\n");
        html.append("</head>\n");
        html.append("<body>\n");
        html.append("    <div class=\"container\">\n");
        html.append("        <div class=\"header\">\n");
        html.append("            <h1>").append(fileName).append("</h1>\n");
        html.append("            <p>PDF文档转换结果</p>\n");
        html.append("        </div>\n");
        html.append("        <div class=\"content\">\n");
        html.append(escapeHtml(text));
        html.append("        </div>\n");
        html.append("        <div class=\"footer\">\n");
        html.append("            <p>转换时间: ").append(java.time.LocalDateTime.now()).append("</p>\n");
        html.append("            <p>使用Apache PDFBox转换</p>\n");
        html.append("        </div>\n");
        html.append("    </div>\n");
        html.append("</body>\n");
        html.append("</html>");
        
        return html.toString();
    }

    /**
     * HTML转义
     * @param text 原始文本
     * @return 转义后的文本
     */
    private String escapeHtml(String text) {
        return text.replace("&", "&amp;")
                  .replace("<", "&lt;")
                  .replace(">", "&gt;")
                  .replace("\"", "&quot;")
                  .replace("'", "&#39;");
    }

    /**
     * 保存HTML到文件
     * @param html HTML内容
     * @param outputPath 输出路径
     * @throws IOException IO异常
     */
    public void saveHtmlToFile(String html, String outputPath) throws IOException {
        Path path = Paths.get(outputPath);
        Files.createDirectories(path.getParent());
        Files.write(path, html.getBytes("UTF-8"));
    }
} 